package com.android.datasave;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
import android.widget.ToggleButton;
import android.widget.Button;

public class datasaveActivity extends Activity {
	/** Called when the activity is first created. */
	public DataService ds;
	public Intent iDs;
	public datasaveActivity _this = this;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		final ToggleButton start = (ToggleButton) findViewById(R.id.toggleButton1);
		final Button read = (Button) findViewById(R.id.button1);
		// final Button write = (Button) findViewById(R.id.button2);
		final Button delete = (Button) findViewById(R.id.button3);
		final Button exit = (Button) findViewById(R.id.button4);
		//final View graph = (View) findViewById(R.id.view1);
		// -- Action Button 2
		read.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				LibFile.ReadFile(getApplicationContext());
			}
		});

		// -- Action Button 3
		delete.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				LibFile.DeleteFile(getApplicationContext());
			}
		});

		// -- Action Button 4
		exit.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});
		
		// ************* BORDEL ********************//
		
		start.setOnClickListener(new View.OnClickListener() {	
			public void onClick(View v) {
				Context context = getApplicationContext();
				AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
				Intent service = new Intent(datasaveActivity.this,DataService.class);
				PendingIntent pendingIntent = PendingIntent.getService(datasaveActivity.this, 0, service, 0);
				
					if(start.isChecked()){
						context.startService(service);
						context.bindService(service, null, BIND_AUTO_CREATE);
						bindService(getIntent(), null, 0);
					}
				
					while(start.isChecked()) {
						Calendar calendar = Calendar.getInstance();
						calendar.setTimeInMillis(System.currentTimeMillis());
						calendar.add(Calendar.SECOND, Config.intervalleRafraichissement);
						alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),pendingIntent);					
					} 
					
					Toast.makeText(getApplicationContext(), "test fail", Toast.LENGTH_SHORT).show();
					alarmManager.cancel(pendingIntent);
					context.unbindService(null);
					context.stopService(service);
			}
		});
		

		/* Dessin du graphique
		 * 
		Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);
		Canvas c = new Canvas(b);
		Paint p = new Paint();
		
		c.drawRect(5, 5, 10,10, p);
		graph.draw(c);
		c.drawRect(5, 5, 10,10, p);
		*
		*/

	}
}
